<!DOCTYPE html>
<html>

<head>
    <title>编辑游记</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/editTravels.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css" />
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/plugins/form/jquery.form.js"></script>
    <!-- 插件核心 -->
    <script src="/js/plugins/eleditor/Eleditor.min.js">
    </script>

    <!-- 如果需要图片上传 -->
    <script src="/js/plugins/eleditor/webuploader.min.js">
    </script>
    <script src="/js/My97DatePicker/WdatePicker.js"></script>
    <script src="/js/common.js"></script>

    <script>
        $(function () {
            //创建编辑器
            var Edr = new Eleditor({
                el: '#contentEditor',
                upload:{
                    server: '/images',
                }
            });


            //更换封面绑定点击事件
            $("#changeBtn").click(function () {
                //用代码的形式去触发点击图片上传表单
                $("#fileInput").click();
            });

            //更换封面 	给上传图片的文本框绑定改变事件
            $("#fileInput").change(function () {
                //有值才提交
                if ($(this).val()) {
                    //ajax提交表单
                    $("#fileForm").ajaxSubmit(function (data) {
                        //上传成功
                        if (data.status == 1) {
                            //回显给用户看
                            $("#coverUrl").attr("src",data.url);
                            //并且设置提交表单的头像隐藏域
                            $("#detailForm input[name='coverUrl']").val(data.url);
                        }
                    });
                }
            });


            var temp="";
            //获取所有地区 发送get请求
            $.get("/regions",function (data) {
                //拼接
                $.each(data,function (index,ele) {
                    temp += "<option value='"+ele.id+"'>"+ele.name+"</option>";
                });
                //添加到下拉框中
                $("#regionSelec").html(temp);


                //获取url上的参数
               var parms = getParams();
                //有参数id的值才去发请求查询(编辑回显)
                if (parms.id) {
                    //设置表单地址
                    $("#detailForm").attr("action","/travels/"+parms.id);
                    //设置请求
                    $("#detailForm input[name='_method']").val("put");
                    //查询个人游记 回显
                    $.get("/travels/"+parms.id,function (data) {
                        console.log(data);
                        $(".travels").renderValues(data);
                    });
                }

            });

            //ajaxfrom 异步提交
            $("#detailForm").ajaxForm({
                beforeSubmit:function(arr){
                    arr.push({name:"title",value:$("#title").val()});//标题
                    arr.push({name:"coverUrl",value:$("#coverUrl").attr("src")});//封面
                    arr.push({name:"travelContent.content",value:Edr.getContent()});//内容
                    arr.push({name:"state",value:state});//状态
                    console.log(arr);
                },
                success:function (data) {
                    if (data.success) {
                        window.location.href = "/questionAndAnswers.html"
                    }
                }
            })

            var state=0;
            //保存
            $("#save").click(function () {
                //提交
                $("#detailForm").submit();
            })

            //保存
            $("#release").click(function () {
                state=1;
                //提交
                $("#detailForm").submit();
            });



          /*  //保存
            $("#save").click(function () {

                var content = Edr.getContent();
                console.log(content);
                //游记内容
                $("#detailForm input[name='travelContent.content']").val(content);
                //题目
                $("#detailForm input[name='title']").val( $("#title").val());
                //状态 草稿
                state = 0;
                //提交
                 $("#detailForm").submit();
            });

            //发布
            $("#release").click(function () {
                var content = Edr.getContent();
                console.log(content);
                //游记内容
                $("#detailForm input[name='travelContent.content']").val(content);
                //题目
                $("#detailForm input[name='title']").val( $("#title").val());
                //状态 待审核
                state = 1;
                //提交
                $("#detailForm").submit();

            });
*/








        });

    </script>


</head>

<body>
    <!--上传图片的表单-->
    <form id="fileForm" method="post" action="/images" enctype="multipart/form-data">
        <!--隐藏的文件筐-->
        <input id="fileInput" type="file" name="file" style="display:none">
    </form>

<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:window.history.go(-1);">
                <span><i class="fa fa-times"></i></span>
            </a>
        </div>
        <div class="col-5"></div>
        <div class="col" style="text-align: right;">
            <i class="fa fa-floppy-o fg-fw" id="save"><span>保存</span></i>
            <i class="fa fa-check" id="release"><span>发布</span></i>
        </div>
    </div>
</div>

<div class="travels form-group">
    <div class="cover">
        <img id="coverUrl" width="100%" height="200px" render-src="coverUrl"/>
       
            <button type="button" class="btn" id="changeBtn">
                <i class="fa fa-pencil-square-o"></i>
                <small>更换封面</small>
            </button>
       

    </div>
    <div class="title">
        <input class="form-control" id="title" render-value="title" placeholder="请输入标题"/>
    </div>
    <h6 href="#">点击文章内容即可编辑</h6>
    <div id="contentEditor" render-html="travelContent.content">
    </div>
    <hr/>
    <div class="container">
        <form id="detailForm" action="/travels" method="post" >
            <!--用隐藏域来传递封面信息-->
            <!--<input type="hidden" name="coverUrl" render-value="coverUrl">
            <input type="hidden" name="travelContent.content" >
            <input type="hidden" name="id" />
            <input type="hidden" name="title" />-->
            <input type="hidden" name="_method"><!--修改表单提交方法  修改的时候-->
            <label>出行时间:</label>
            <input type="text" name="travelTime"  class="form-control" onclick="WdatePicker()" render-value="travelTime">
            <label>出行天数:</label>
            <input type="number" name="days" class="form-control" render-value="days">
            <label>参与人物:</label>
            <select class="c-select form-control" name="person" render-value="person" >
                <option value="1" selected>一个人的旅行</option>
                <option value="2">和父母</option>
                <option value="3">和朋友</option>
                <option value="4">和同事</option>
                <option value="5">和爱人</option>
                <option value="6">和其他</option>
            </select>
            <label>人均费用:</label>
            <input type="text" name="perExpends" class="form-control" render-value="perExpends">

            <label>旅游地点:</label>
            <select class="form-control" id="regionSelec" name="place.id" render-value="place.id" >

            </select>

            <div class="form-check">
                <label class="form-check-label">
                    <input class="form-check-input" type="checkbox"  name="isPublic" render-attr="checked=isPublic">是否公开
                </label>
            </div>

        </form>
    </div>

    <hr/>

</div>


</body>

</html>